package com.mall.server.controller.ums;

import com.mall.model.domain.UmsMenu;
import com.mall.model.domain.UmsResource;
import com.mall.model.domain.UmsRole;
import com.mall.model.vo.CommonPage;
import com.mall.model.vo.CommonResult;
import com.mall.server.service.ums.UmsRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/role")
public class UmsRoleController {

    @Autowired
    private UmsRoleService umsRoleService;


    /**
     * 根据角色名称分页获取角色列表
     */
    @GetMapping("/list")
    public CommonResult list(String keyword
            , @RequestParam(defaultValue = "1") Integer pageNum
            , @RequestParam(defaultValue = "10") Integer pageSize) {

        CommonPage cp = umsRoleService.page(keyword, pageNum, pageSize);

        return CommonResult.success(cp);
    }

    /**
     * 添加角色
     */
    @PostMapping("/create")
    public CommonResult createRole(@RequestBody UmsRole role) {

        umsRoleService.createRole(role);
        return CommonResult.success(null);
    }

    /**
     * 给角色分配菜单
     */
    @PostMapping("/allocMenu")
    public CommonResult allocMenu(Integer[] menuIds, Integer roleId) {

        umsRoleService.allocMenu(menuIds, roleId);
        return CommonResult.success(null);
    }

    /**
     * 给角色分配资源
     */
    @PostMapping("/allocResource")
    public CommonResult allocResource(Integer[] resourceIds, Integer roleId) {

        umsRoleService.allocResource(resourceIds, roleId);
        return CommonResult.success(null);
    }

    /**
     * 批量删除角色
     */
    @PostMapping("/delete")
    public CommonResult delete(@RequestParam("ids") List<Integer> ids) {

        umsRoleService.delete(ids);

        return CommonResult.success(null);
    }

    /**
     * 获取所有角色
     */
    @GetMapping("/listAll")
    public CommonResult listAll() {

        List<UmsRole> list = umsRoleService.listAll();

        return CommonResult.success(list);
    }

    /**
     * 获取角色相关菜单
     */
    @GetMapping("/listMenu/{roleId}")
    public CommonResult listMenu(@PathVariable Integer roleId) {

        List<UmsMenu> list = umsRoleService.listMenu(roleId);

        return CommonResult.success(list);
    }

    /**
     * 获取角色相关资源
     */
    @GetMapping("/listResource/{roleId}")
    public CommonResult listResource(@PathVariable Integer roleId) {

        List<UmsResource> list = umsRoleService.listResource(roleId);

        return CommonResult.success(list);
    }

    /**
     * 修改角色
     */
    @PostMapping("/update/{id}")
    public CommonResult updateRole(@PathVariable Integer id, @RequestBody UmsRole role) {

        umsRoleService.updateRole(id, role);

        return CommonResult.success(null);
    }

    /**
     * 修改角色状态
     */
    @PostMapping("/updateStatus/{id}")
    public CommonResult updateStatus(@PathVariable Integer id, Integer status) {

        umsRoleService.updateStatus(id, status);

        return CommonResult.success(null);
    }


}
